package inheritance;

import java.util.Arrays;

public class MyCharSequence implements CharSequence{

	private char[] string;
	
	public MyCharSequence(String string) {
		if (string == null) {
			throw new NullPointerException("parameter string cannot be null!");
		}
		
		this.string = string.toCharArray();
	}
	
	@Override
	public int length() {
		return string.length;
	}

	@Override
	public char charAt(int index) {
		if (index >= 0) {
			return string[index];
		}
		
		throw new IndexOutOfBoundsException("index="+index);
	}

	@Override
	public CharSequence subSequence(int start, int end) {
		String string = String.valueOf(Arrays.copyOfRange(this.string, start, end));
		return new MyCharSequence(string);
	}
	
	@Override
	public String toString() {
		return new String(this.string);
	}

	public static void main(String[] args) {
		String string = "Select one of the sentences from this book to use as the data.";
		CharSequence sequence = new MyCharSequence(string);
		System.out.println(sequence);
		System.out.println(sequence.length());
		System.out.println(sequence.charAt(11));
		
		CharSequence sequence2 = sequence.subSequence(5, 10);
		System.out.println(sequence2);
	}
}
